/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Swing;

import enums.CarEnum;
import javax.swing.JTextField;
import common.CustomerUtils;
import common.CarUtils;
import java.util.HashMap;
import enums.CustomerEnum;
import java.awt.Color;
import javax.swing.JOptionPane;
import org.apache.commons.lang.math.NumberUtils;

public class CarsDialog extends javax.swing.JDialog {
    CustomerUtils customerUtils;
    CarUtils carUtils;
    public static final String CAR_ID = "carId";
    private final String YEAR_OF_PROD_MESSAGE = "Data produkcji samochodu musi się składać z 4 liczb";
    private final String YEAR_OF_PROD_ERROR = "Błąd danych wejściowych roku produkcji samochodu";
    private final String YEAR_OF_PROD_FORMAT_ERROR = "Niepoprawny format roku produkcji";
    
    public CarsDialog(java.awt.Frame parent, boolean modal, int idCustomer) {
        super(parent, modal);
        this.idCustomer = idCustomer;
        initComponents();
        customerUtils = new CustomerUtils();
        carUtils = new CarUtils();
        populateCustomerLabels(idCustomer);
        setTitle("Dodawanie nowego pojazdu");
    }
    public CarsDialog(java.awt.Frame parent, int idCar, boolean modal) {
        super(parent, modal);
        this.idCar = idCar;
        initComponents();
        carUtils = new CarUtils();
        populateCarAndCustomerLabels(idCar);
        setTitle("Edycja danych pojazdu");
    }
    private void populateCustomerLabels(int customerId){
       HashMap<String, String> map = customerUtils.getCustomerByIdAsHashMap(customerId);
       lblCarCustFirstName.setText(map.get(CustomerEnum.firstName.name()));
       lblCarCustLastName.setText(map.get(CustomerEnum.lastName.name()));
       lblCarCustStreet.setText(map.get(CustomerEnum.street.name()));
       lblCarCustCity.setText(map.get(CustomerEnum.city.name())); 
    }
    private void populateCarAndCustomerLabels(int carId){
       HashMap<String, String> map = carUtils.getCarByIdAsHashMap(carId);
       tbCarMark.setText(map.get(CarEnum.mark.name()));
       tbCarType.setText(map.get(CarEnum.type.name()));
       tbCarYearOfProduction.setText(map.get(CarEnum.yearOfProduction.name()));
       lblCarCustFirstName.setText(map.get(CustomerEnum.firstName.name()));
       lblCarCustLastName.setText(map.get(CustomerEnum.lastName.name()));
       lblCarCustStreet.setText(map.get(CustomerEnum.street.name()));
       lblCarCustCity.setText(map.get(CustomerEnum.city.name())); 
    }
    
    private boolean validateAllFields(){
        boolean isValid = true;
        if(tbCarMark.getText().isEmpty()){
            isValid = false;
            lblCarMarkWarn.setForeground(Color.red);
            lblCarMarkWarn.setText("Marka jest wymagana");
        }
        else{
            isValid = true;
            lblCarMarkWarn.setText("");
        }
        
        if(tbCarType.getText().isEmpty()){
            isValid = false;
            lblCarTypeWarn.setForeground(Color.red);
            lblCarTypeWarn.setText("Typ jest wymagany");
        }
        else{
           isValid = true;
           lblCarTypeWarn.setText("");
        }
        
        if(!tbCarYearOfProduction.getText().isEmpty()){ 
            if(tbCarYearOfProduction.getText().length() != 4){
                 JOptionPane.showMessageDialog(null, YEAR_OF_PROD_MESSAGE, YEAR_OF_PROD_ERROR, JOptionPane.ERROR_MESSAGE);
                 isValid = false;
            }
    
            if(!NumberUtils.isDigits(tbCarYearOfProduction.getText())){
                JOptionPane.showMessageDialog(null, YEAR_OF_PROD_FORMAT_ERROR, YEAR_OF_PROD_ERROR, JOptionPane.ERROR_MESSAGE);
                isValid = false;
            }

        
        }
        return isValid;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnSaveCar = new javax.swing.JButton();
        lblCarMark = new javax.swing.JLabel();
        tbCarMark = new javax.swing.JTextField();
        lblCarType = new javax.swing.JLabel();
        tbCarType = new javax.swing.JTextField();
        lblCarYearProduction = new javax.swing.JLabel();
        tbCarYearOfProduction = new javax.swing.JTextField();
        lblCarMarkWarn = new javax.swing.JLabel();
        lblCarTypeWarn = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblCarCustFirstName = new javax.swing.JLabel();
        lblCarCustLastName = new javax.swing.JLabel();
        lblCarCustStreet = new javax.swing.JLabel();
        lblCarCustCity = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dane pojazdu"));

        btnSaveCar.setText("Zapisz");
        btnSaveCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveCarActionPerformed(evt);
            }
        });

        lblCarMark.setText("Marka pojazdu:");

        lblCarType.setText("Model (typ):");

        lblCarYearProduction.setText("Rok produkcji:");

        tbCarYearOfProduction.setColumns(4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblCarType, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tbCarType))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblCarMark, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tbCarMark, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCarMarkWarn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblCarTypeWarn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblCarYearProduction, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbCarYearOfProduction, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(btnSaveCar, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbCarMark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCarMark)
                    .addComponent(lblCarMarkWarn))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbCarType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCarType)
                    .addComponent(lblCarTypeWarn))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbCarYearOfProduction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCarYearProduction))
                .addGap(18, 18, 18)
                .addComponent(btnSaveCar)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dane Klienta"));

        jLabel1.setText("Imię:");

        jLabel2.setText("Nazwisko:");

        jLabel3.setText("Ulica:");

        jLabel4.setText("Miejscowość:");

        lblCarCustFirstName.setText("jLabel5");

        lblCarCustLastName.setText("jLabel5");

        lblCarCustStreet.setText("jLabel5");

        lblCarCustCity.setText("jLabel5");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCarCustFirstName)
                            .addComponent(lblCarCustLastName, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addComponent(lblCarCustStreet, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(lblCarCustCity))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblCarCustFirstName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblCarCustLastName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblCarCustStreet))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblCarCustCity))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveCarActionPerformed
        if(validateAllFields()){
            HashMap<String, String> map = new HashMap<>();
            map.put(CarEnum.mark.name(), tbCarMark.getText());
            map.put(CarEnum.type.name(), tbCarType.getText());
            map.put(CarEnum.yearOfProduction.name(), tbCarYearOfProduction.getText());
            map.put(CustomerEnum.idCustomer.name(), String.valueOf(idCustomer));
            if(this.idCar != 0){
                map.put(CAR_ID, String.valueOf(idCar));
                carUtils.saveModifiedCar(map);
            }
            else{
                carUtils.saveNewCar(map);
            }
            
            setVisible(false);
            dispose();
        }
    }//GEN-LAST:event_btnSaveCarActionPerformed


        /* Set the Nimbus look and feel 
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
    /*
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CustomDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CustomDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustomDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CustomDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
*/
        //</editor-fold>


    private int idCustomer = 0;
    private int idCar = 0;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSaveCar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCarCustCity;
    private javax.swing.JLabel lblCarCustFirstName;
    private javax.swing.JLabel lblCarCustLastName;
    private javax.swing.JLabel lblCarCustStreet;
    private javax.swing.JLabel lblCarMark;
    private javax.swing.JLabel lblCarMarkWarn;
    private javax.swing.JLabel lblCarType;
    private javax.swing.JLabel lblCarTypeWarn;
    private javax.swing.JLabel lblCarYearProduction;
    private javax.swing.JTextField tbCarMark;
    private javax.swing.JTextField tbCarType;
    private javax.swing.JTextField tbCarYearOfProduction;
    // End of variables declaration//GEN-END:variables
}
